package com.taoym.voucher.xcx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taoym.voucher.common.enums.ImageInfoTypeEnum;
import com.taoym.voucher.common.utils.ThreadLocalUtils;
import com.taoym.voucher.xcx.domain.XcxImageInfo;
import com.taoym.voucher.xcx.mapper.XcxImageInfoMapper;
import com.taoym.voucher.xcx.service.XcxImageInfoService;
import com.taoym.voucher.xcx.service.XcxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @coder taoym
 * @datetime 2021/7/13 21:32
 * @desc 图片信息接口服务实现类
 */
@Service
public class XcxImageInfoServiceImpl implements XcxImageInfoService {

    @Resource
    private XcxImageInfoMapper xcxImageInfoMapper;
    @Autowired
    private XcxService xcxService;

    @Override
    public String getMyPoster() {

        String uid = ThreadLocalUtils.get().getUid();

        XcxImageInfo xcxImageInfo = xcxImageInfoMapper.selectOne(new QueryWrapper<XcxImageInfo>()
                .select("url")
                .eq("uid", uid)
                .eq("type", ImageInfoTypeEnum.MINI_CODE.getCode()));
        if (ObjectUtils.isEmpty(xcxImageInfo)) {
            String qrCode = xcxService.createQRCode(uid);
            xcxImageInfo = new XcxImageInfo();
            xcxImageInfo.setUid(uid);
            xcxImageInfo.setType(ImageInfoTypeEnum.MINI_CODE.getCode());
            xcxImageInfo.setUrl(qrCode);

            xcxImageInfoMapper.insert(xcxImageInfo);
            return qrCode;
        }
        return xcxImageInfo.getUrl();
    }

    @Override
    public List<String> listImages(Integer type) {
        List<XcxImageInfo> xcxImageInfos = xcxImageInfoMapper.selectList(new QueryWrapper<XcxImageInfo>()
                .select("url").eq("type", type));
        if (CollectionUtils.isEmpty(xcxImageInfos)) {
            return null;
        }
        return xcxImageInfos.stream().map(XcxImageInfo::getUrl).collect(Collectors.toList());
    }
}


